package com.example.wujinapi.securityservice;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wujinapi.dao.AdminsDao;
import com.example.wujinapi.entity.Admins;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service("securityMyUserDetailService")
public class SecurityMyUserDetailService implements UserDetailsService {

    @Autowired
    private AdminsDao adminsDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //查询用户信息
        Admins adminuser=adminsDao.selectOne(new QueryWrapper<Admins>().eq("adminname",username));

        //用户不存在
        if(Objects.isNull(adminuser)){
            System.out.println("用户名异常");
            throw new RuntimeException("用户名异常");
        }

        //查询对应的权限信息
        List<String> perminssionList=new ArrayList<>(Arrays.asList("user:admin"));

        //把数据封装成UserDetails.返回
        //return new SecurityLoginUser(adminuser);
        return new SecurityLoginUser(adminuser,perminssionList);
    }

}
